package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ponhu.ea2.entity.PhSeries;
import com.ponhu.ea2.enums.ResponseCodeEnum;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhSeriesMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhSeriesService;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class PhSeriesServiceImpl
        extends ServiceImpl<PhSeriesMapper, PhSeries>
        implements PhSeriesService {

    @Override
    public PhSeries getPhSeriesByBrandIdAndName(Long brandId, String name) {
        return this.getOne(Wrappers.lambdaQuery(PhSeries.class)
                .eq(PhSeries::getBrandId, brandId)
                .eq(PhSeries::getName, name)
                .orderByDesc(PhSeries::getLevel).last("limit 1"));
    }

    @Override
    public PhSeries checkSeriesByName(String series, Long brandId) {
        if (StringUtils.isBlank(series)) {
            return new PhSeries();
        }
        // 根据名称查询系列,如果没有则创建
        return list(new LambdaQueryWrapper<PhSeries>()
                .eq(PhSeries::getName, series.trim())
                .eq(PhSeries::getBrandId,brandId)
                .orderByDesc(PhSeries::getLevel))
                .stream().findFirst().orElseGet(() -> createPhSeries(series, brandId));
    }

    @Override
    public PhSeries updateSerieaName(String series, Long seriesId, Long brandId) {
        PhSeries phSeries = new PhSeries();
        if (seriesId != null) {
            phSeries.setId(seriesId);
            phSeries.setName(series);
            if (!updateById(phSeries)) {
                throw new BusinessRuntimeException(ResponseCodeEnum
                        .SERIES_EDIT_EXISTS_800802);
            }
        } else {
            phSeries = checkSeriesByName(series, brandId);
        }
        return phSeries;
    }

    public PhSeries createPhSeries(String series, Long brandId) {
        PhSeries phSeries = new PhSeries();
        phSeries.setBrandId(brandId);
        phSeries.setName(series);
        phSeries.setCreatedTime(new Date());
        if (!save(phSeries)) {
            throw new BusinessRuntimeException(ResponseCodeEnum
                    .SERIES_SAVE_EXISTS_800801);
        }
        return phSeries;
    }

    @Override
    public List<PhSeries> getListByBrandIds(List<Long> brandIds) {
        if (CollectionUtils.isEmpty(brandIds)) return Lists.newArrayList();

        LambdaQueryWrapper<PhSeries> queryWrapper = Wrappers
                .lambdaQuery(PhSeries.class).in(PhSeries::getBrandId, brandIds);

        return list(queryWrapper);
    }

}
